package com.hyt.it.ogt.kq.common.bm.swagger;

import com.hyt.core.util.GeneralProperties;
import com.hyt.swagger.ApiVersion;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import springfox.documentation.builders.ApiInfoBuilder;
import springfox.documentation.builders.ParameterBuilder;
import springfox.documentation.builders.PathSelectors;
import springfox.documentation.schema.ModelRef;
import springfox.documentation.service.ApiInfo;
import springfox.documentation.service.Contact;
import springfox.documentation.service.Parameter;
import springfox.documentation.spi.DocumentationType;
import springfox.documentation.spring.web.plugins.Docket;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

@Configuration
public class SwaggerApiConfig {
	
    @SuppressWarnings("deprecation")
	@Bean
    public Docket v2312Api() {
    	ParameterBuilder tokenPar = new ParameterBuilder();  
        List<Parameter> pars = new ArrayList<Parameter>();  
        tokenPar.name("access_token")
        	.description("token")
        	.modelRef(new ModelRef("string"))
        	.parameterType("query")
        	.required(false).build();  
        pars.add(tokenPar.build());
    	return new Docket(DocumentationType.SWAGGER_2)
                .groupName(BmApiVersionConstant.V2_3_12)
                .apiInfo(v2312apiInfo())
                .select()
                .apis(input -> {
					ApiVersion apiVersion = input.getHandlerMethod().getMethodAnnotation(ApiVersion.class);
                    if(apiVersion != null && Arrays.asList(apiVersion.group()).contains(BmApiVersionConstant.V2_3_12)){
                        return true;
                    }
                    return false;
                })
                .paths(PathSelectors.any())
                .build()
                .globalOperationParameters(pars);
    }
    
    @SuppressWarnings("deprecation")
    @Bean
    public Docket v2411Api() {
        ParameterBuilder tokenPar = new ParameterBuilder();  
        List<Parameter> pars = new ArrayList<Parameter>();  
        tokenPar.name("access_token")
            .description("token")
            .modelRef(new ModelRef("string"))
            .parameterType("query")
            .required(false).build();  
        pars.add(tokenPar.build());
        return new Docket(DocumentationType.SWAGGER_2)
                .groupName(BmApiVersionConstant.V2_4_11)
                .apiInfo(v2411apiInfo())
                .select()
                .apis(input -> {
                    ApiVersion apiVersion = input.getHandlerMethod().getMethodAnnotation(ApiVersion.class);
                    if(apiVersion != null && Arrays.asList(apiVersion.group()).contains(BmApiVersionConstant.V2_4_11)){
                        return true;
                    }
                    return false;
                })
                .paths(PathSelectors.any())
                .build()
                .globalOperationParameters(pars);
    }
    
    @SuppressWarnings("deprecation")
    @Bean
    public Docket v24p18Api() {
        ParameterBuilder tokenPar = new ParameterBuilder();  
        List<Parameter> pars = new ArrayList<Parameter>();  
        tokenPar.name("access_token")
            .description("token")
            .modelRef(new ModelRef("string"))
            .parameterType("query")
            .required(false).build();  
        pars.add(tokenPar.build());
        return new Docket(DocumentationType.SWAGGER_2)
                .groupName(BmApiVersionConstant.V2_4_p18)
                .apiInfo(v24p18apiInfo())
                .select()
                .apis(input -> {
                    ApiVersion apiVersion = input.getHandlerMethod().getMethodAnnotation(ApiVersion.class);
                    if(apiVersion != null && Arrays.asList(apiVersion.group()).contains(BmApiVersionConstant.V2_4_p18)){
                        return true;
                    }
                    return false;
                })
                .paths(PathSelectors.any())
                .build()
                .globalOperationParameters(pars);
    }
    
    private ApiInfo v2312apiInfo() {
        String title=GeneralProperties.getProperty("hyt.api.doc.title","海云天云考试");
        String description=GeneralProperties.getProperty("hyt.api.doc.description","海云天云考试系统对外接口");
        String termsOfServiceUrl=GeneralProperties.getProperty("hyt.api.doc.termsOfServiceUrl","http://www.seaskylight.com");
        String contactName=GeneralProperties.getProperty("hyt.api.doc.contact.name","云考试-考试项目组");
        String contactUrl=GeneralProperties.getProperty("hyt.api.doc.contact.url","http://www.seaskylight.com");
        String contactEamil=GeneralProperties.getProperty("hyt.api.doc.contact.email","liuq@seaskylight.com");
        return new ApiInfoBuilder().title(title)
                .description(description)
                .termsOfServiceUrl(termsOfServiceUrl)
                .contact(new Contact(contactName, contactUrl, contactEamil))
                .version(BmApiVersionConstant.V2_3_12)
                .build();
    }
	
    
    
    private ApiInfo v2411apiInfo() {
        String title=GeneralProperties.getProperty("hyt.api.doc.title","海云天云考试");
        String description=GeneralProperties.getProperty("hyt.api.doc.description","海云天云考试系统对外接口");
        String termsOfServiceUrl=GeneralProperties.getProperty("hyt.api.doc.termsOfServiceUrl","http://www.seaskylight.com");
        String contactName=GeneralProperties.getProperty("hyt.api.doc.contact.name","云考试-考试项目组");
        String contactUrl=GeneralProperties.getProperty("hyt.api.doc.contact.url","http://www.seaskylight.com");
        String contactEamil=GeneralProperties.getProperty("hyt.api.doc.contact.email","liuq@seaskylight.com");
        return new ApiInfoBuilder().title(title)
                .description(description)
                .termsOfServiceUrl(termsOfServiceUrl)
                .contact(new Contact(contactName, contactUrl, contactEamil))
                .version(BmApiVersionConstant.V2_4_11)
                .build();
    }
    
    private ApiInfo v24p18apiInfo() {
        String title=GeneralProperties.getProperty("hyt.api.doc.title","海云天云考试");
        String description=GeneralProperties.getProperty("hyt.api.doc.description","海云天云考试系统对外接口");
        String termsOfServiceUrl=GeneralProperties.getProperty("hyt.api.doc.termsOfServiceUrl","http://www.seaskylight.com");
        String contactName=GeneralProperties.getProperty("hyt.api.doc.contact.name","云考试-考试项目组");
        String contactUrl=GeneralProperties.getProperty("hyt.api.doc.contact.url","http://www.seaskylight.com");
        String contactEamil=GeneralProperties.getProperty("hyt.api.doc.contact.email","liuq@seaskylight.com");
        return new ApiInfoBuilder().title(title)
                .description(description)
                .termsOfServiceUrl(termsOfServiceUrl)
                .contact(new Contact(contactName, contactUrl, contactEamil))
                .version(BmApiVersionConstant.V2_4_p18)
                .build();
    }

}
